package com.yhao.uerservice.controller;

import com.yhao.uerservice.bean.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

/**
 * @author yhao
 * @create 2018-02-12 11:00
 */

@RestController
public class UserController {

    private final Logger logger = LoggerFactory.getLogger(getClass());


    @Autowired
    private DiscoveryClient discoveryClient;

    @GetMapping(value = "/user")
    public String user() {
        ServiceInstance instance = discoveryClient.getLocalServiceInstance();
        logger.info("host: " + instance.getHost() + " , service_id: " + instance.getServiceId()
                + " , thread: " + Thread.currentThread());

        return "hello user~";
    }

    @GetMapping(value = "/api.test/1")
    public String user2() {
        ServiceInstance instance = discoveryClient.getLocalServiceInstance();
        logger.info("host: " + instance.getHost() + " , service_id: " + instance.getServiceId()
                + " , thread: " + Thread.currentThread());

        return "hello user~";
    }



    @GetMapping(value = "/user1")
    String hello(@RequestParam("name") String name) {
        return "hello " + name;
    }

    @GetMapping(value = "/user2")
    User hello(@RequestHeader String name, @RequestHeader int age) {
        return new User(name, age);
    }

    @RequestMapping(value = "/user3")
    String hello(@RequestBody() User user) {
        return "hello " + user.getName() + ", " + user.getAge();
    }
}
